package fi.foyt.cs.foursquare;

import fi.foyt.cs.Settings;
import fi.foyt.cs.api.SystemProperty;
import fi.foyt.foursquare.api.FoursquareApi;
import fi.foyt.foursquare.api.FoursquareApiException;
import fi.foyt.foursquare.api.entities.Category;
import fi.foyt.foursquare.api.io.GAEIOHandler;

public class FoursquareUtils {

  public static FoursquareApi getFoursquareApi() {
    String clientId = Settings.getValue(SystemProperty.FOURSQUARE_CLIENTID);
    String clientSecret = Settings.getValue(SystemProperty.FOURSQUARE_CLIENTSECRET);
    String returnUrl = Settings.getValue(SystemProperty.FOURSQUARE_RETURNURL);
    
    return new FoursquareApi(clientId, clientSecret, returnUrl, new GAEIOHandler());
  }

  public static Category getCategory(String id) throws FoursquareApiException {
    // TODO: Cache
    
    return getCategory(getFoursquareApi().venuesCategories(), id);
  }
  
  private static Category getCategory(Category[] categories, String id) {
    for (Category category : categories) {
      if (id.equals(category.getId())) {
        return category;
      }
      
      return getCategory(category.getCategories(), id);
    }
    
    return null;
  }
}
